package cn.edu.sgu.www.mhxysy.controller.children;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.children.ChildrenDTO;
import cn.edu.sgu.www.mhxysy.entity.children.Children;
import cn.edu.sgu.www.mhxysy.pager.children.ChildrenPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.children.ChildrenService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "子女管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/children", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ChildrenController {

    private final ChildrenService childrenService;

    @Autowired
    public ChildrenController(ChildrenService childrenService) {
        this.childrenService = childrenService;
    }

    @ApiOperation("初始化子女技能")
    @RequestMapping(value = "/init", method = RequestMethod.POST)
    public JsonResult<Void> init() {
        childrenService.init();

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("添加子女")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated ChildrenDTO childrenDTO) {
        childrenService.insert(childrenDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID删除子女")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "子女ID", required = true)
            @NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
        childrenService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改子女信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated ChildrenDTO childrenDTO) {
        childrenService.updateById(childrenDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("查询全部子女")
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public JsonResult<List<Children>> selectAll() {
        List<Children> list = childrenService.selectAll();

        return JsonResult.success(null, list);
    }

    @ApiOperation("通过ID查询子女信息")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public JsonResult<Children> selectById(
            @ApiParam(name = "id", value = "子女ID", required = true)
            @NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
        Children children = childrenService.selectById(id);

        return JsonResult.success(null, children);
    }

    @ApiOperation("分页查询子女列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
    public JsonResult<JsonPage<Children>> selectByPage(ChildrenPager pager) {
        Page<Children> page = childrenService.selectByPage(pager);

        return JsonResult.restPage(page);
    }

}